<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>超市管理系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', Arial, sans-serif;
        }
        
        body {
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-image: linear-gradient(to right bottom, #3498db, #2ecc71);
        }
        
        .login-container {
            width: 400px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            padding: 30px;
            text-align: center;
        }
        
        .login-logo {
            margin-bottom: 20px;
        }
        
        .login-logo h1 {
            color: #3498db;
            font-size: 28px;
            margin-bottom: 5px;
        }
        
        .login-logo p {
            color: #777;
            font-size: 14px;
        }
        
        .login-form {
            text-align: left;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 600;
            color: #333;
        }
        
        .form-control {
            width: 100%;
            padding: 10px 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        .form-control:focus {
            border-color: #3498db;
            outline: none;
        }
        
        .form-check {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }
        
        .form-check input {
            margin-right: 10px;
        }
        
        .login-btn {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .login-btn:hover {
            background-color: #2980b9;
        }
        
        .footer-text {
            margin-top: 20px;
            font-size: 12px;
            color: #777;
        }

        .error-message {
            color: #e74c3c;
            font-size: 14px;
            margin-top: 10px;
            text-align: center;
            display: none;
        }

        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 10px;
            vertical-align: middle;
            display: none;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-logo">
            <h1>超市管理系统</h1>
            <p>Supermarket Management System</p>
        </div>
        <form class="login-form" id="login-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" class="form-control" placeholder="请输入用户名" required>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" class="form-control" placeholder="请输入密码" required>
            </div>
            <div class="form-check">
                <input type="checkbox" id="remember-me">
                <label for="remember-me">记住我</label>
            </div>
            <button type="submit" class="login-btn">
                <span class="loading" id="loading-spinner"></span>
                登录系统
            </button>
            <div class="error-message" id="error-message"></div>
        </form>
        <p class="footer-text"></p>
    </div>
    
    <script>
        document.getElementById('login-form').addEventListener('submit', function(e) {
            e.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const errorMessage = document.getElementById('error-message');
            const loadingSpinner = document.getElementById('loading-spinner');

            // 重置错误信息
            errorMessage.style.display = 'none';
            errorMessage.textContent = '';
            // 显示加载状态
            loadingSpinner.style.display = 'inline-block';

            // 调用后端登录接口
            fetch('/api/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ username, password })
            })
            .then(res => res.json().then(data => ({ status: res.status, body: data })))
            .then(({ status, body }) => {
                if (status < 400 && body.success) {
                    const userData = {
                        username: username,
                        role: 'admin',
                        lastLogin: new Date().toISOString()
                    };
                    if (document.getElementById('remember-me').checked) {
                        localStorage.setItem('user', JSON.stringify(userData));
                    } else {
                        sessionStorage.setItem('user', JSON.stringify(userData));
                    }
                    window.location.href = 'index.html';
                } else {
                    errorMessage.textContent = body.message || '登录失败';
                    errorMessage.style.display = 'block';
                }
            })
            .catch(err => {
                errorMessage.textContent = '网络错误';
                errorMessage.style.display = 'block';
            })
            .finally(() => {
                loadingSpinner.style.display = 'none';
            });
        });

        // 检查用户是否已登录
        window.addEventListener('DOMContentLoaded', function() {
            const user = JSON.parse(localStorage.getItem('user') || sessionStorage.getItem('user') || 'null');
            if (user) {
                // 用户已登录，直接跳转到首页
                window.location.href = 'index.html';
            }
        });
    </script>
</body>
</html> 